

var gameManager = require("gameManager");

cc.Class({
  extends: cc.Component,

  properties: {
    rankItemTemplate: {
      default: null,
      type: cc.Prefab
    },
    scrollView: {
      default: null,
      type: cc.ScrollView
    },
    totalCount: 20, // how many items we need for the whole list
    spacing: 20, // space between each item

  },

  onLoad: function () {
    this.content = this.scrollView.content;
    this.test();
  },

  update: function (dt) {
  },

  initialize: function (scoreReplys) {
    //this.frontUiName = frontUiName;
    // this.content.height = this.totalCount * (this.rankItemTemplate.height + this.spacing) + this.spacing;
    this.content.height = this.spacing;
    if (scoreReplys != null) {
      for (let i = 0; i < scoreReplys.length; ++i) {
        var scoreReply = scoreReplys[i]
        if (scoreReply != null) {
          let item = cc.instantiate(this.rankItemTemplate);
          this.content.height += item.height + this.spacing;
          this.content.addChild(item);
          item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));

          var rankUiItemSc = item.getComponent("rankUiItem");
          item.active = true;
          rankUiItemSc.updateBaseScore(scoreReply);
          // item.getComponent('Item').updateItem(i, i);
          //     this.items.push(item);
        }
      }
    }
  },

  onCloseButtonClicked: function () {
    this.node.destroy();
    gameManager.instance.returnToStartUi();
    // if(frontUiName == ""){
    //   gameManager.returnToStartUi();
    // }else if(frontUiName == ""){
    //   gameManager.returnToStartUi();
    // }else {

    // }
  },

  updateBaseScore: function (scoreReplys) {
    //this.maxScorePlayer.string =  maxScore;
    this.initialize(scoreReplys);
  },
  
  test: function() {
    cc.log("---------------- rankuictrl test start ----------------")
    var scoreReplys = {};
    scoreReplys.length = 20;
    for(let i=0; i<20;i++){
      var scoreReply = {};
      scoreReply.rank = i;
      scoreReply.nickName = "name" + i ;
      scoreReply.score = i * 10;
      //scoreReply.avatarURL
      scoreReplys[i] = scoreReply;
    }
    this.initialize(scoreReplys);
    cc.log("---------------- rankuictrl test end ----------------")
  }


});
